(function() {
    'use strict';

    /**
     * 各种小的组件集合
     */

    angular.module('webApp')
        .directive('userTitle', function() {
            return {
                restrict: 'E',
                template: "{{titles[user.Role]}}{{version}}",
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', 'sessionService', '$timeout', 'cacheService', function($templateCache, $rootScope, $scope, $compile, dataService, sessionService, $timeout, cacheService) {
                    $scope.titles = {
                        1: "买家",
                        2: "卖家",
                        3: "供应商",
                        4: "管理员",
                    }

                    $scope.user = sessionService.get("tokenUser");

                    // 5定制版6旗舰版7基础版8官网版9体验版
                    $scope.version_name = {
                        "9": "体验版",
                        "8": "官网版",
                        "7": "基础版",
                        "6": "旗舰版",
                        "5": "尊享版"
                    }
                    var query = { partner_id: $scope.user.companyId };
                    dataService.v1PartnerGet(query).then(function(lit) {
                        if (!lit.Msg) {
                            if (!lit.order_version) {
                                console.error("你没有订购服务，无法使用");
                                return false;
                                // $scope.fwPayment(true);
                            } else {
                                if ($scope.currentTime > $scope.endTime(lit.serve_end_at)) {
                                    // $scope.fwPayment(true);
                                } else {
                                    //体验版
                                    if (lit.order_version == 9) {
                                        // $scope.fwPayment(true);
                                    } else {
                                        $scope.version = "--" + $scope.version_name[lit.order_version];
                                    }
                                }
                            }
                        }
                    });

                    //当前时间
                    $scope.currentTime = function() {
                        var a = (new Date()).toLocaleDateString();
                        a = a.replace(/\//g, '-');
                        return (new Date(a)) / 1000;
                    }

                    //结束时间
                    $scope.endTime = function(time) {
                        var date = new Date(time);
                        return date.getTime() / 1000;
                    }

                    $scope.fwPayment = function(usecache) {
                        var questKey = "fwPayment" + $rootScope.currentUser.open_id;
                        var ttl = 15;
                        var cacheVal = cacheService.get(questKey, ttl);
                        if (usecache && cacheVal) {
                            // console.log("缓存：24小时在弹出", questKey);
                        } else {
                            cacheService.set(questKey, 1, ttl);
                            var qrcodeWin = $.alert({
                                backdrop: "static",
                                keyboard: false,
                                title: '{{"技术支持"|translate}}',
                                body: '<fuwu-list type="all"></fuwu-list>',
                                okBtn: '{{"关闭"|translate}}',
                                closeBtn: true,
                                hasfoot: false,
                                transition: true,
                                width: 'normal',
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                },
                                okHide: function(e) {},
                                shown: function(e) {}
                            });
                            // console.log("没有查到缓存：", cacheService.get(questKey, ttl));
                        }
                    }
                }]
            }
        })
        .directive('eaform', function($compile) {
            return {
                restrict: 'E',
                replace: true,
                // scope: true,
                // require: '^eaa',
                template: '<ul>' +
                    '<li >' +
                    '<div ng-show="imgsrc" style="margin-left: 20px;margin-top:10px" ><span>图片</span> <attachment owner-id="1" owner-type="uimods" dhf-ng-model="imgsrc" xtype="url" mult="false" title="修改图片" use-type="png,jpeg,gif,jpg,JPG,PNG,JPEG,GIF" setback="imgsrc_c(attach)"></attachment><br/><img ng-src="{{imgsrc}}" style="width:210px;height:135px;margin-right: 20px;margin-top: 10px;" /></div>' +
                    '<div style="margin-left: 20px;margin-bottom:10px"><br/><a-url setback="href_c(path,ctype,cid,cover)"></a-url></div><br/>' +
                    '<div style="margin-left: 20px;margin-bottom:10px"><br/>高度:<input type="number" ng-change="setImageHeight()" ng-model="imgheight" value="100" min="0" /></div><br/>' +
                    '</li>' +
                    '</ul>',
                link: function(scope, element, attributes) {
                    // scope.ind = Math.round(Math.random() * 100);
                    scope.remove = function() {
                        // console.log(scope.ind);
                        element.remove();
                    }
                }
            }
        })

    .directive('ea', function($compile) {
        return {
            scope: {
                href: "@",
            },
            restrict: 'C',
            controller: ['$templateCache', '$rootScope', '$scope', function($templateCache, $rootScope, $scope) {}],
            link: function(scope, element, attrs, mainCtrl) {
                var img = angular.element(element).find('img');
                if (img[0]) {
                    scope.imgsrc = img[0].src;
                    scope.imgheight = img[0].height;
                    scope.cimage = img[0];
                }
                element.bind("mouseenter", function() {
                    element.addClass("editorable2");
                    angular.element(document).find('.editbtn2').remove();
                    var $newDirective = angular.element('<div class="editbtn2">' +
                        '<i ng-click="remove()" class="fa fa-times fa-1 removebtn rbt2"></i>' +
                        '</div>');
                    element.append($newDirective);
                    $compile($newDirective)(scope);
                });
                element.bind("mouseleave", function() {
                    // console.error("mouseleave/////////");
                    element.removeClass("editorable2");
                    angular.element(document).find('.editbtn2').remove();
                });

                element.bind("click", function() {
                    scope.getData();
                });

                element.on('click', function(event) {
                    event.preventDefault();

                    angular.element(document).find('.editing').removeClass("editing");
                    element.addClass("editing");
                });

                scope.html = function() {
                    // console.error("html:", element.html());
                }

                scope.remove = function() {
                    // console.error("element",element);
                    element.remove();
                    event.preventDefault();
                }

                scope.getData = function() {

                    var $newDirective = angular.element('<eaform>');
                    angular.element(document).find('#formbox').html($newDirective);
                    $compile($newDirective)(scope);
                }

                scope.href_c = function(path, ctype, cid, cover) {
                    // console.error("=================",path,ctype,cid,cover);
                    // element.attr('href', scope.href);
                    if (path) {
                        element.attr('cpath', path);
                        element.attr('href', path);
                    }
                    if (ctype) {
                        element.attr('ctype', ctype);
                    }
                    if (cid) {
                        element.attr('cid', cid);
                    }
                    if (cover) {
                        element.attr('cover', cover);
                    }
                }

                scope.setImageHeight = function() {
                    scope.cimage.height = scope.imgheight;
                    var hh = scope.imgheight > 0 ? scope.imgheight : "";
                    angular.element(element).find('img').css('height', hh);
                    // console.error("设置高度：", scope.imgheight, scope.cimage);
                }

                scope.imgsrc_c = function(attach) {
                    // console.error("hrefhref", attach);
                    angular.element(element).find('img').attr('src', attach);
                }

            }
        }
    })

    .directive('udpanelchild', ['$compile', function($compile) {
        return {
            restrict: 'C',
            link: function(scope, element, attrs, mainCtrl) {
                element.bind("mouseenter", function() {
                    // console.error("mouseenter.........");
                    // console.error("html:", element.html());
                    element.addClass("editorable1");
                    angular.element(document).find('.editbtn1').remove();
                    var $newDirective = angular.element('<div class="editbtn1">' +
                        '<i ng-click="remove()" class="fa fa-times fa-3 removebtn rbt1"></i>' +
                        '</div>');
                    element.append($newDirective);
                    $compile($newDirective)(scope);
                });

                element.bind("mouseleave", function() {
                    // console.error("mouseleave/////////");
                    element.removeClass("editorable1");
                    angular.element(document).find('.editbtn1').remove();
                });

                scope.remove = function() {
                    // console.error("element";
                    element.remove();
                    // element.html(" ");
                    event.preventDefault();
                }
            }
        }
    }])

    .directive('bindHtmlCompile', ['$compile', function($compile) {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                scope.$watch(function() {
                    return scope.$eval(attrs.bindHtmlCompile);
                }, function(value) {
                    // Incase value is a TrustedValueHolderType, sometimes it
                    // needs to be explicitly called into a string in order to
                    // get the HTML string.
                    element.html(value && value.toString());
                    // If scope is provided use it, otherwise use parent scope
                    var compileScope = scope;
                    if (attrs.bindHtmlScope) {
                        compileScope = scope.$eval(attrs.bindHtmlScope);
                    }
                    $compile(element.contents())(compileScope);
                });
            }
        };
    }]);
})();